//
//  UITableViewController+HDContentTenementController.m
//  app
//
//  Created by D on 15/8/20.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDContentTenementController.h"
#import "SNRepairAskController.h"
#import "HDRepairListController.h"
#import "SNContentTenmentDataManager.h"
#import "HDChatOBO.h"

@interface HDContentTenementController ()
@property (nonatomic, retain) SNContentTenmentDataManager * manager;
@property (nonatomic, retain) SNContentTenmentResult * result;
@end

@implementation HDContentTenementController


-(void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.title = @"联系物业";
    
    self.result = [[SNContentTenmentResult alloc] initWithTarget:self action:@selector(loadMyDataWithResult:)];
    self.manager = [SNContentTenmentDataManager shareManager];
    
    self.tableView.nothingRefreshView = [[UIRefreshNotingRefreshView alloc] initWithView:self.tableView viewController:self tryAgain:@selector(tryAgain)];
    [self.tableView.nothingRefreshView show];
    [self.manager loadDataWithResult:self.result];
}

- (void)tryAgain
{
    [self.manager loadDataWithResult:self.result];
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch ([indexPath section]) {
        case 1:
        {
            if (indexPath.row == 0) {
                
                if ([[[NSUserDefaults standardUserDefaults]objectForKey:kMyAreaID]integerValue] > 0) {
                    //在线咨询
                    HDChatOBO * chatVC = [[HDChatOBO alloc]init];
                    chatVC.chat_people_face = self.result.model.customerServicesModel.face;
                    chatVC.chat_people_name = self.result.model.customerServicesModel.nickname;
                    chatVC.chat_people_uid = self.result.model.customerServicesModel.uid;
                    [self.navigationController pushViewController:chatVC animated:YES];

                }else
                {
                    UIAlertView * av = [[UIAlertView alloc]initWithTitle:@"请先选择一个小区" message:nil delegate:self cancelButtonTitle:@"我知道了" otherButtonTitles:nil, nil];
                    [av show];
                }
                
                
                
                
            }
            else if (indexPath.row == 1){
            //设备报修
                SNRepairAskController * repairAskVC = [[SNRepairAskController alloc] init];
                repairAskVC.repairAskModel = self.result.model;
                [self.navigationController pushViewController:repairAskVC animated:YES];
            }
            else if (indexPath.row == 2){
            //报修列表
                
//                if ([[[NSUserDefaults standardUserDefaults]objectForKey:kMyAreaID]integerValue] > 0) {
                
                    HDRepairListController * repairListVC = [[HDRepairListController alloc]init];
                    repairListVC.mall_com_id = self.result.model.mall_com_id;
                    repairListVC.fullAddress = [[NSUserDefaults standardUserDefaults]objectForKey:@"fullAddress"];
                    [self.navigationController pushViewController:repairListVC animated:YES];
                    
//                }else
//                {
//                    UIAlertView * av = [[UIAlertView alloc]initWithTitle:@"请先选择小区" message:nil delegate:self cancelButtonTitle:@"朕知道了" otherButtonTitles:nil, nil];
//                    [av show];
//                }
                
            }
        }
            break;
            
        default:
            break;
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)loadMyDataWithResult:(SNContentTenmentResult *)result
{
    if (result.success) {
        [self.tableView.nothingRefreshView remove];
        [self.phoneBtn setTitle:[NSString stringWithFormat:@"%@",result.model.tel] forState:UIControlStateNormal];
        [self.phoneBtn setTitleColor:[XCommon hexStringToColor:@"#F72643"] forState:UIControlStateNormal];
        [self.tableView reloadData];
    } else {
        [MyToast showDefaultBottonWithText:@"请确保您已经选择了小区.如果您已经选择了小区,请点击重新加载..."];
        [self.tableView.nothingRefreshView requestAgain];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (IBAction)callPhone:(id)sender {
    
    NSString * phoneNumber = [NSString stringWithFormat:@"telprompt:%@",self.result.model.tel];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
    
}
@end
